<template>
    <el-container class="home-container">
      <!--头部-->
      <el-header>
        <div>
          <img src="../assets/logo.png" alt="" width="50px" height="50px">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!--页面主体区域-->
      <el-container>
        <!--侧边栏-->
        <el-aside :width="isCol?'64px':'200px'">
          <div class="toggle-button" @click = "toggleCol">|||</div>
          <el-menu
            :collapse-transition="false"
            background-color="dimgray"
            text-color="#fff"
            active-text-color="#409EFF"
            unique-opened
            :collapse="isCol"
            router
            :default-active="activePath"
          >
            <!--一级菜单-->
            <el-submenu :index='item.id+""' v-for='item in menuList' :key='item.id'>
              <!--一级菜单模板区-->
              <template slot="title">
                <!--图标-->
                <i :class="iconO[item.id]"></i>
                <!--文本-->
                <span>{{item.authName}}</span>
              </template>
              <!--二级菜单-->
              <el-menu-item :index='"/"+child.path' v-for='child in item.children' :key='child.id' @click="saveNavState('/'+child.path)">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>{{child.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!--右侧内容-->
        <el-main>
          <router-view ></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  name: 'home',
  data () {
    return {
      menuList: [],
      iconO: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-tools',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-data'
      },
      isCol: false,
      // 被激活链接地址
      activePath: ''
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      // 获取所有的菜单
      // eslint-disable-next-line no-unused-vars
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      console.log(res)
    },
    toggleCol () { // 点击菜单，进行折叠或展开
      this.isCol = !this.isCol
    },
    saveNavState (activePath) { // 保存链接的激活状态
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  created () { // 生命周期函数
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
  .el-header{
  height: 80px !important;
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 5px;
  padding-left: 15px;
  padding-right: 15px;
  align-items: center;
  color: white;
  font-size: 25px;
  >div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
  .el-aside{
    background-color: dimgray;
    .el-menu{
      border-right: none;
    }
  }
  .el-main{
    background-color: #dddddd;
  }
  .home-container{
    background-color: aquamarine;
    height: 100%;
  }
  .toggle-button{
    background-color: #2b4b6b;
    font-size: 15px;
    line-height: 30px;
    color: white;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
